from selenium.webdriver import ActionChains
from hyrobot.common import STEP, CHECK_POINT, INFO
from lib.webui_permission import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

class c73248d2:
    name = '验证所有界面存在且可操作产品管理表格中按钮部分-c73248d2'  # 测试用例名字

    # 初始化方法
    def setup(self):
        STEP(1, '登陆网站')
        driver = open_browser()
        super_login(driver)

    # 清除方法
    def teardown(self):
        driver = get_global_webdriver()
        driver.quit()

    # 测试用例步骤
    def teststeps(self):
        # 获取webdriver对象 对应 已经登录好的浏览器
        driver = get_global_webdriver()
        # driver = webdriver.Firefox()

        extending_button = driver.find_element_by_id("hamburger-container")
        try:
            driver.find_element(By.CLASS_NAME, "el-tooltip")
            extending_button.click()
        except:
            print("左侧栏没有隐藏")
        # click product manage
        time.sleep(1)
        product_manage_word2 = driver.find_element(By.XPATH,
                                                   "/html/body/div/div/div[1]/ul/div[3]/a/li/span"
                                                   )
        product_manage_word2.click()
        time.sleep(1)
        top_title2 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span[2]/span[1]/span"
                                         ).text
        CHECK_POINT("上方第3个标题对应", "产品管理" == top_title2)

        STEP(2, "点击查看")
        time.sleep(1)
        product_name = driver.find_element(By.CSS_SELECTOR,
                                           'div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)'
                                           )
        product_name.clear()
        product_name.send_keys('chanpin')
        time.sleep(1)
        # click select
        driver.find_element(By.CSS_SELECTOR,
                            'div.el-form-item:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)'
                            ).click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            'button.el-button--mini:nth-child(1) > span:nth-child(1)'
                             ).click()
        time.sleep(1)
        input_box = driver.find_elements(By.CLASS_NAME,"el-input__inner")
        input_box[0].send_keys("xiangmu")
        applyer = "孙熙东"
        input_box[1].send_keys('sunxidong')
        # click select
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--primary:nth-child(1) > span:nth-child(1)"
                            ).click()
        got_applyer = driver.find_element(By.CSS_SELECTOR,
                                          "tr.el-table__row:nth-child(1) > td:nth-child(4) > div:nth-child(1)"
                                          ).text
        CHECK_POINT("检查申请人是否正确",got_applyer == applyer)
        # check detail
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "tr.el-table__row:nth-child(1) > td:nth-child(11) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        got_title = driver.find_element(By.CSS_SELECTOR,
                                        "div.el-dialog__wrapper:nth-child(4) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)"
                                        ).text
        CHECK_POINT("内部标题是否一致",got_title == "申请记录信息")
        time.sleep(1)
        # close
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-dialog__footer:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        # 回去产品管理
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "span.el-breadcrumb__item:nth-child(2) > span:nth-child(1) > a:nth-child(1)"
                            ).click()

        # 编辑
        STEP(3,"进行编辑")
        time.sleep(2)
        driver.find_element(By.CSS_SELECTOR,
                            'tr.el-table__row:nth-child(1) > td:nth-child(7) > div:nth-child(1) > button:nth-child(2)'
                            ).click()
        edit_product(driver)

        # delete
        STEP(4,"删除")
        time.sleep(1)
        super_name = get_username(4)
        # remember delete product following
        uploader = driver.find_element(By.CSS_SELECTOR,
                                       "div.el-form-item:nth-child(2) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)"
                                       ).send_keys(super_name)
        # click select
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-form-item:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(3)
        driver.find_element(By.CSS_SELECTOR,
                            "tr.el-table__row:nth-child(1) > td:nth-child(7) > div:nth-child(1) > button:nth-child(3) > span:nth-child(1)"
                            ).click()
        # concel
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            ".el-message-box__btns > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "tr.el-table__row:nth-child(1) > td:nth-child(7) > div:nth-child(1) > button:nth-child(3) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--small:nth-child(2)"
                            ).click()
